<template>
  <div class="container">
    <div class="mb10">
      <el-text class="mx-1"> 查询条件：</el-text>
    </div>
    <el-row :gutter="10">
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-input v-model.trim="searchCondition.order_sn" placeholder="xxxx订单号" clearable></el-input>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-input v-model.trim="searchCondition.investor_name" placeholder="投资商名称" clearable></el-input>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-select v-model.trim="searchCondition.type" placeholder="请选择是操作类型" clearable>
          <el-option v-for="item in statusList" :label="item.dictLabel" :value="item.dictValue" :key="item.id"></el-option>
        </el-select>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-date-picker v-model="queryDate" type="datetimerange" range-separator="至" format="YYYY-MM-DD HH:mm:ss" value-format="x" start-placeholder="开始时间" @change="changeQueryDate" end-placeholder="结束时间"> </el-date-picker>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-space wrap>
          <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
          <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
        </el-space>
      </el-col>
    </el-row>
    <div class="mb10">
      <el-text class="mx-1"> 查询结果：</el-text>
    </div>
    <el-space wrap class="mb20">
      <el-button type="primary" :icon="View" :disabled="disabledbtn" @click="handleViewCountData">查看报表数据</el-button>
    </el-space>
    <el-table :data="tableList" border class="table" row-key="id" ref="multipleTableRef" v-loading="loading" :reserve-selection="true" @row-dblclick="rowDblclick">
      <el-table-column type="index" align="center"> </el-table-column>
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="order_sn" label="xxxx订单号" align="center"> </el-table-column>
      <el-table-column prop="investor_name" label="投资商名称" align="center"> </el-table-column>
      <el-table-column prop="type_name" label="资金操作类型" align="center"> </el-table-column>
      <el-table-column prop="money" label="金额/元" align="center"> </el-table-column>
      <el-table-column prop="rest_money" label="账户余额/元" align="center"> </el-table-column>
      <el-table-column prop="remark" label="备注" align="center"> </el-table-column>
      <el-table-column prop="master_name" label="操作人" align="center"> </el-table-column>
      <el-table-column prop="operation_time" label="预计投资时间" align="center" :formatter="(row: IInvestorsMoneyLogInfo) => handleVerifyFormatDate(Number(row.operation_time))"> </el-table-column>
      <el-table-column prop="create_time" label="记录创建时间" align="center" :formatter="(row: IInvestorsMoneyLogInfo) => handleVerifyFormatDate(Number(row.create_time))"> </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        @update:current-page="handleCurrentChange"
        @update:page-size="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="searchCondition.size"
        :current-page="searchCondition.current"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        :total="total"
        :hide-on-single-page="false">
      </el-pagination>
    </div>
    <InvestorMoneyLogCountDialog v-model:edit-visible="dialogVisible" />
  </div>
</template>

<script setup lang="ts">
import InvestorMoneyLogCountDialog from "@/components/user/investorsManager/investorMoneyLogInfo/InvestorMoneyLogCountDialog.vue";
import { Search, Refresh, View } from "@element-plus/icons-vue";
import { handleVerifyFormatDate } from "@/utils/utils";
import { useState } from "@/hooks/useState.ts";
import type { IDictData } from "@/types/dict";

import type { IInvestorsMoneyLogInfo, ISearchInvestorsMoneyLogInfo } from "@/types/user/investorMoneyLogInfo";
import { getInvestorMoneyLogInfo } from "@/utils/api/investorMoneyLog";
interface Props {
  statusList: Array<IDictData>;
}
const { statusList } = defineProps<Props>();
const { disabledbtn, loading, toggleState } = useState();

/* 暴露一个方法给父组件使用 */
defineExpose({
  /* 这一步关键，在setup语法糖中使用时 */
  getTableInfo
});

const multipleTableRef = useTemplateRef("multipleTableRef"); /* 列表组件的引用 */

const total = ref(0);
const dialogVisible = ref(false);
const searchCondition = reactive<ISearchInvestorsMoneyLogInfo>({
  order_sn: "",
  investor_name: "",
  type: "",
  start_time: "",
  end_time: "",
  current: 1,
  size: 10
});

const queryDate = ref<Array<string>>([]);
const tableList = ref<Array<IInvestorsMoneyLogInfo>>([]);

onMounted(() => {
  getTableInfo();
});
/* 查询 创建时间更改事件 */
function changeQueryDate(data: number[]) {
  if (data !== null) {
    searchCondition.start_time = data[0];
    searchCondition.end_time = data[1];
  } else {
    searchCondition.start_time = "";
    searchCondition.end_time = "";
  }
}
/* 搜索 */
function searchInfo() {
  searchCondition.current = 1;
  searchCondition.size = 10;
  getTableInfo();
}

/**
 * 重置搜索内容
 */
function resetSearch() {
  searchCondition.order_sn = "";
  searchCondition.investor_name = "";
  searchCondition.type = "";
  searchCondition.start_time = "";
  searchCondition.end_time = "";
  queryDate.value = [];
  searchCondition.current = 1;
  searchCondition.size = 10;

  getTableInfo();
}

/* 查看汇总统计的数据 */
function handleViewCountData() {
  dialogVisible.value = true;
}
/* 请求：获取表格中数据 */
function getTableInfo() {
  toggleState(true);
  getInvestorMoneyLogInfo<IInvestorsMoneyLogInfo, ISearchInvestorsMoneyLogInfo>(searchCondition)
    .then(res => {
      tableList.value = res.result.data;
      total.value = res.result.total;
    })
    .finally(() => {
      toggleState(false);
    });
}

/* 显示第几页 */
function handleCurrentChange(val: number) {
  searchCondition.current = val;
  getTableInfo();
}

/* 每页显示几条 */
function handleSizeChange(val: number) {
  searchCondition.current = 1;
  searchCondition.size = val;
  getTableInfo();
}

/* 双击当前行 */
function rowDblclick(row: IInvestorsMoneyLogInfo) {
  multipleTableRef.value!.toggleRowSelection(row);
}
</script>

<style lang="scss" scoped>
/* layout-布局 */
.el-col {
  border-radius: 4px;
}

.mb10 {
  margin-bottom: 10px;
}
/* 关键：列表右侧操作按钮的对其 */
:deep(.el-space__item) {
  line-height: 15px;
}

.el-button.is-text + .el-button.is-text {
  margin-left: 0;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}

.table {
  width: 100%;
  font-size: 14px;
}
.textAlignCenter {
  text-align: center;
}

/* 确定按钮的显示与隐藏 */
.displaybtn {
  display: none;
}

/* 提示图标设置 */
.icon-wzg-config {
  height: 30px;
  line-height: 30px;
}

.w-tag .el-tag {
  margin: 2px 5px;
}

.el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
</style>
